// Package controllers
// @Title  menu.go
// @Description  菜单控制器
// @Author  小斌 (时间 格式是2021/12/02  11:40)
// @Update  小斌 (时间 格式是2021/12/02  11:40)

package controllers

import (
	"beego-admin/models"
	"beego-admin/service"
	"encoding/json"
)

type MenuController struct {
	BaseController
}

// AjaxGetMenu 获取左侧菜单
func (this *MenuController) AjaxGetMenu() {
	menuJson := this.GetSession("menu_json")
	var JsonRes service.LeftMenuJson
	_ = json.Unmarshal([]byte(menuJson.(string)), &JsonRes)
	this.Data["json"] = JsonRes
	this.ServeJSON()
}

// MenuList 菜单列表页
func (this *MenuController) MenuList() {
	this.TplName = "admin/system_setting/menu_list.tpl"
}

// GetAllMenuList  获取全部菜单数据
func (this *MenuController) GetAllMenuList() {
	menuList,count,_ := models.GetMenuList(9999,1,"")


	this.Data["json"] = this.AjaxLayuiTableReturn(0,"成功",count,menuList)
	this.ServeJSON()
}

// GetTreeMenuList 获取树形菜单
func (this *MenuController) GetTreeMenuList() {
	menuList,count,_ := models.GetMenuList(9999,1,"")
	var returnMenuList []service.MenuList
	for _,v := range *menuList{
		pId := v.PId
		if v.PId == 0{
			pId = -1
		}
		menuType := "菜单"
		if v.Type == "button"{
			menuType = "按钮"
		}
		returnMenuList = append(returnMenuList,service.MenuList{
			v.Id,
			v.Title,
			v.Sort,
			v.Href,
			v.Icon,
			0,
			pId,
			menuType,
		})
	}
	this.Data["json"] = this.AjaxLayuiTableReturn(0,"成功",count,returnMenuList)
	this.ServeJSON()
}